#![allow(dead_code)]
use tracing::{subscriber::set_global_default, Subscriber};
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer;
use tracing_subscriber::{fmt::MakeWriter, layer::SubscriberExt, EnvFilter, Registry};

//异步日志
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
    LogTracer::init().expect("Failed to set logger");
    set_global_default(subscriber).expect("Failed to set subscriber");
}
pub fn get_subscriber<Field, Sink>(
    name: String,
    skip_fileds: impl Iterator<Item = Field>,
    filter_level: String,
    sink: Sink,
) -> impl Subscriber + Send + Sync
where
    Field: Into<String>,
    Sink: for<'a> MakeWriter<'a> + Send + Sync + 'static,
{
    let env_filter =
        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(filter_level));
    let format_layer = BunyanFormattingLayer::new(name, sink)
        .skip_fields(skip_fileds)
        .expect("One of the specified fields cannot be skipped");

    Registry::default()
        .with(env_filter)
        .with(JsonStorageLayer)
        .with(format_layer)
}
//同步日志
